#include <stdio.h>
#include <libconfig.h>
#include <libconfig.h>

void write_config(config_t *cfg)
{
    config_set_tab_width(cfg, 4);  // 设置制表符宽度为4个空格
    config_set_options(cfg, CONFIG_OPTION_FSYNC);  // 写入文件时强制将缓存中的数据写入磁盘
    config_set_options(cfg, CONFIG_OPTION_ALLOW_OVERRIDES);  // 重复属性时覆盖旧值
    config_set_options(cfg, CONFIG_OPTION_SEMICOLON_SEPARATORS);  // 允许使用分号作为分隔符
    // 将配置写入文件
    if (!config_write_file(cfg, "reliabledisk.conf")) {
        fprintf(stderr, "Can't write to file: %s\n", config_error_text(cfg));
        config_destroy(cfg);
        return;
    }
}

int read_config(config_t *cfg)
{
    const char *str = NULL;
    config_setting_t *setting_blacklist;
    setting_blacklist = config_lookup(cfg, "blacklist.name");
    if (!setting_blacklist) {
        fprintf(stderr, "No 'blacklist' setting in configuration file.\n");
        return 1;
    }
    if (config_setting_is_array(setting_blacklist)) {
        fprintf(stdout, "config_setting_is_array\n");
    }
    str = config_setting_get_string_elem(setting_blacklist, 0);
    if (str) {
        printf("blacklist.name: %s\n", str); 
    }
    return 0;
}

int main() {
    config_t cfg;

    // 初始化配置库
    config_init(&cfg);
    config_read_file(&cfg, "reliabledisk.conf");
    read_config(&cfg);
    config_destroy(&cfg);  // 清理配置库
    return 0;
}